const { 
  TheClass,
  Student,
  Course,
  ExperimentCourse,
  Teacher,
  Laboratory,
  ExperimentPlan,
  ExperimentScore,
  ExperimentTotalScore
} = require('../model');

async function createAllTable() {
  await ExperimentTotalScore?.drop();
  await ExperimentScore?.drop();
  await ExperimentPlan?.drop();
  await Laboratory?.drop();
  await Teacher?.drop();
  await ExperimentCourse?.drop();
  await Course?.drop();
  await Student?.drop();
  await TheClass?.drop();
  
  await TheClass.hasMany(Student);
  await Student.belongsTo(TheClass, {
    foreignKey: 'tcId',
  });
  await TheClass.sync({ force: true })
  .then(() => {
    console.log('TheClass 表建立成功');
  })
  .catch(err => {
    console.log('TheClass 表建立失败', err);
  });
  await Student.sync({ force: true })
  .then(() => {
    console.log('Student 表建立成功');
  })
  .catch(err => {
    console.log('Student 表建立失败', err);
  });
  
  await Course.hasMany(ExperimentCourse);
  await ExperimentCourse.belongsTo(Course, {
    foreignKey: 'cId',
  });
  await Course.sync({ force: true })
  .then(() => {
    console.log('Course 表建立成功');
  })
  .catch(err => {
    console.log('Course 表建立失败', err);
  });
  await ExperimentCourse.sync({ force: true })
  .then(() => {
    console.log('ExperimentCourse 表建立成功');
  })
  .catch(err => {
    console.log('ExperimentCourse 表建立失败', err);
  });

  await Teacher.sync({ force: true })
  .then(() => {
    console.log('Teacher 表建立成功');
  })
  .catch(err => {
    console.log('Teacher 表建立失败', err);
  });

  await Laboratory.sync({ force: true })
  .then(() => {
    console.log('Laboratory 表建立成功');
  })
  .catch(err => {
    console.log('Laboratory 表建立失败', err);
  });

  await ExperimentCourse.hasMany(ExperimentPlan);
  await ExperimentPlan.belongsTo(ExperimentCourse, {
    foreignKey: 'ecId',
  });
  await Laboratory.hasMany(ExperimentPlan);
  await ExperimentPlan.belongsTo(Laboratory, {
    foreignKey: 'lId',
  });
  await ExperimentPlan.sync({ force: true })
  .then(() => {
    console.log('ExperimentPlan 表建立成功');
  })
  .catch(err => {
    console.log('ExperimentPlan 表建立失败', err);
  });

  await ExperimentCourse.hasMany(ExperimentScore);
  await ExperimentScore.belongsTo(ExperimentCourse, {
    foreignKey: 'ecId',
  });
  await Student.hasMany(ExperimentScore);
  await ExperimentScore.belongsTo(Student, {
    foreignKey: 'sId',
  });
  await ExperimentScore.sync({ force: true })
  .then(() => {
    console.log('ExperimentScore 表建立成功');
  })
  .catch(err => {
    console.log('ExperimentScore 表建立失败', err);
  });

  await Course.hasMany(ExperimentTotalScore);
  await ExperimentTotalScore.belongsTo(Course, {
    foreignKey: 'cId',
  });
  await Student.hasMany(ExperimentTotalScore);
  await ExperimentTotalScore.belongsTo(Student, {
    foreignKey: 'sId',
  });
  await ExperimentTotalScore.sync({ force: true })
  .then(() => {
    console.log('ExperimentTotalScore 表建立成功');
  })
  .catch(err => {
    console.log('ExperimentTotalScore 表建立失败', err);
  });
}

// createAllTable();
module.exports = createAllTable;
